﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;

namespace HarveyNash.RefactoringAndCodeSmell.CodeSmell.LargeClass.After
{
    public static class CryptographyHelper
    {
        public static string Base64StringEncode(string input)
        {
            byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(input);
            return Convert.ToBase64String(encbuff);
        }

        public static string Base64StringDecode(string input)
        {
            byte[] decbuff = Convert.FromBase64String(input);
            return System.Text.Encoding.UTF8.GetString(decbuff);
        }

        public static string MD5String(string input)
        {
            MD5 md5Hasher = MD5.Create();

            byte[] data = md5Hasher.ComputeHash(
               Encoding.Default.GetBytes(input));

            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }

        public static bool MD5VerifyString(string input, string hash)
        {
            string hashOfInput = MD5String(input);

            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static string SHA1String(string input)
        {
            SHA1 sha1Hasher = SHA1.Create();

            byte[] data = sha1Hasher.ComputeHash(
               Encoding.Default.GetBytes(input));

            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }

        public static bool SHA1VerifyString(string input, string hash)
        {
            string hashOfInput = SHA1String(input);

            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            return 0 == comparer.Compare(hashOfInput, hash);
        }
    }
}
